import os, time, random, math

# 当前登录用户在userinfo.txt文件中的行数
login_index = -1
userinfo_path = os.path.abspath('..') + '\\data\\userinfo.txt'

# 读取用户信息文件，并返回为一个列表对象
def read_user():
    user_file = open(userinfo_path)
    user_list = user_file.readlines()
    return user_list

# 检查用户信息并返回下标
def check_user(username):
    user_list = read_user()
    for i in range(1, len(user_list)):
        if user_list[i].split(',')[0] == username:
            return i

    return -1

# 往userinfo.txt文件追加一条用户信息
def write_userinfo(content):
    user_file = open(userinfo_path, mode='a+')
    user_file.write(content)
    user_file.close()


# 对输入的字符串进行判断，确认其是否可以转换为一个有效的数字
def check_number(string):
    point = 0
    is_valid = True
    for c in string:
        code = ord(c)
        if (code < 48 and code != 46) or code > 57:
            is_valid = False
            break
        elif code == 46: point += 1

    if point > 1: is_valid = False

    return is_valid

# 直接向userinfo.txt文件中修改余额，并写回
def change_balance(user_index, money):
    user_list = read_user()
    temp_list = user_list[user_index].split(',')
    username = temp_list[0]
    password = temp_list[1]
    balance = temp_list[2]
    phone = temp_list[3]

    balance = str(int(balance) + int(money))
    user_list[user_index] = username + ',' + password + ',' + balance + ',' + phone

    user_file = open(userinfo_path, mode='w')
    user_file.writelines(user_list)
    user_file.close()


# 注册
def reg():
    username = input("请输入用户名[注册]：")
    password = input('请输入密码[注册]：')
    phone = input('请输入电话号码[注册]：')

    if check_user(username) >= 0:
        print("你注册的用户名已经存在.")
    else:
        print("恭喜你，用户名可用.")
        user_info = ''
        user_info += username + ','
        user_info += password + ','
        user_info += str(random.randrange(1000, 5000, 100)) + ','
        user_info += phone + '\n'

        write_userinfo(user_info)
        print("恭喜你，注册成功.")
        start_menu()

# 登录
def login():
    username = input("请输入用户名[登录]：")
    password = input('请输入密码[登录]：')

    index = check_user(username)
    if index >= 0:
        user_list = read_user()
        if user_list[index].split(',')[1] == password:
            print("恭喜你，登录成功.")
            global login_index  # 要在函数中修改全局变量，则必须加global进行声明，如果不修改只读取，只可以直接读取
            login_index = index  # 修改全局变量login_index为当前登录用户的下标
            main_menu()
        else:
            print("抱歉，密码输入错误.")
    else:
        print("抱歉，登录用户名不存在.")

# 查询
def query():
    pass


# 存款
def deposite():
    money = input("请输入你要存的钱：")
    if check_number(money):
        change_balance(login_index, int(money))
    else:
        print("请输入正确的金额.")
        deposite()


# 取款
def withdraw():
    pass


# 转账
def transer():
    pass


# 流水
def history():
    pass


# 构建开始菜单
def start_menu():
    tip = '''
***************** 欢迎来到WoniuATM **********************
***************** 请选择操作菜单 ************************
************* 1. 注册   2. 登录   3. 退卡 ***************
    '''
    print(tip)
    option = input("请输入你的操作选项：")
    if option == "1":
        reg()
    elif option == "2":
        login()
    elif option == "3":
        exit('谢谢，欢迎下次光临.')
    else:
        start_menu()

# 构建登录后业务菜单
def main_menu():
    tip = '''
*************************** 恭喜登录蜗牛银行 **********************************
*************************** 请选择操作菜单 ************************************
****** 1. 查询   2. 取款   3. 存款   4. 转账   5. 流水  6. 返回  7. 退卡 *******
        '''
    print(tip)
    option = input("请输入你的操作选项：")
    if option == "1":
        query()
    elif option == "2":
        withdraw()
    elif option == "3":
        deposite()
    elif option == "4":
        transer()
    elif option == "5":
        history()
    elif option == "6":
        start_menu()
    elif option == "7":
        exit('谢谢，欢迎下次光临.')


start_menu()